package com.axialproject.health.exchange.demoacme;

import com.axialproject.health.exchange.ConnectorListener;
import com.axialproject.health.exchange.EMF;
import com.axialproject.health.exchange.PatientManager;
import com.axialproject.health.exchange.Record;
import com.axialproject.health.exchange.entity.Gender;
import com.axialproject.health.exchange.entity.Patient;
import com.axialproject.health.exchange.entity.Race;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

public class ConnectorServlet extends ConnectorListener {

    private static final Logger log = Logger.getLogger(ConnectorServlet.class.getName());

    @Override
    protected Record getRecord(HttpServletRequest req) {
        return null;
    }

    @Override
    protected Patient getPatient(HttpServletRequest req) {
        Patient patient = null;
        EntityManager em = EMF.get().createEntityManager();
        try {
            patient = PatientManager.getPatientByUsernameAppName(em, req.getParameter("username"), getAppName());
            if (patient == null) {
                patient = new Patient ();
                patient.setAppName(getAppName());
                patient.setUsername(req.getParameter("username"));
                patient.setName(req.getParameter("name"));
                patient.setDob(req.getParameter("dob"));
                patient.setAddress(req.getParameter("address"));
                patient.setServiceName(req.getParameter("serviceName"));
                if ("f".equals(req.getParameter("gender")) || "F".equals(req.getParameter("gender"))) {
                    patient.setGender(Gender.FEMALE);
                } else {
                    patient.setGender(Gender.MALE);
                }
                patient.setRace(Race.fromName(req.getParameter("race")));

                // Do not save unknown patient. Just pass it along for MPI matching

            } else {
                // We will update the patient if it is registered
                patient.setName(req.getParameter("name"));
                patient.setDob(req.getParameter("dob"));
                patient.setAddress(req.getParameter("address"));
                patient.setServiceName(req.getParameter("serviceName"));
                if ("f".equals(req.getParameter("gender")) || "F".equals(req.getParameter("gender"))) {
                    patient.setGender(Gender.FEMALE);
                } else {
                    patient.setGender(Gender.MALE);
                }
                patient.setRace(Race.fromName(req.getParameter("race")));
            }
        } catch (Exception e) {
            log.log (Level.SEVERE, e.getMessage(), e);
        } finally {
            em.close ();
        }
        return patient;
    }

    @Override
    protected String[] getDestinations(HttpServletRequest req) {
        ArrayList <String> ds = new ArrayList <String> ();
        ds.add (req.getParameter("referrer"));
        if ("1".equals(req.getParameter("sendToPatient"))) {
            ds.add ("google");
        }
        return ds.toArray(new String [0]);
    }

    @Override
    protected String getAppName() {
        return "demoacme";
    }

}
